/*
 * Copyright (c) 2006-2024, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2024-07-22     RT-Thread    first version
 */

#include <rtthread.h>
#include <rtdevice.h>

#define DBG_TAG "main"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>

#define PIN_RST 32
rt_timer_t app_rst_ctrl_timer = NULL;
uint8_t rst_ctrl_count = 0;
uint8_t is_rst_ctrl = 0;
uint8_t rst_pin_status = 1;

static void app_rst_ctrl_timer_timeout_callback(void *parameter)
{
    if(rst_pin_status) {
        rst_pin_status = 0;

        rt_pin_write(PIN_RST, 1);

    } else {
        rst_pin_status = 1;

        rt_pin_write(PIN_RST, 0);
    }

    rst_ctrl_count++;
    if(rst_ctrl_count > 12) {
        rt_timer_stop(app_rst_ctrl_timer);
        return;
    }
}

void app_rst_ctrl_active(void)
{
    rst_pin_status = 1;
    rst_ctrl_count = 0;
    rt_pin_write(PIN_RST, 1);
    rt_timer_start(app_rst_ctrl_timer);
}

int main(void)
{
    int count = 1;
    rt_pin_mode(PIN_RST, PIN_MODE_OUTPUT);
    rt_pin_write(PIN_RST, 1);
    app_rst_ctrl_timer = rt_timer_create("RST CTRL", app_rst_ctrl_timer_timeout_callback, NULL, 1000, RT_TIMER_FLAG_PERIODIC);

    extern int task_led_init(void);
    extern int task_button_init(void);
    extern int task_pwm_init(void);
    task_led_init();
    task_button_init();
    task_pwm_init();

    while (count++)
    {
        rt_thread_mdelay(1000);
        LOG_D("Hello RT-Thread!");
    }

    return RT_EOK;
}


#define PWM_DEV_NAME        "pwm2" /* PWM设 备 名 称 */
#define PWM_DEV_CHANNEL     1 << 0/* PWM通 道 */
#define PWM_THREAD_PRIORITY         25
#define PWM_THREAD_STACK_SIZE       512
#define PWM_THREAD_TIMESLICE        10
struct rt_device_pwm *pwm_dev; /* PWM设 备 句 柄 */

static void pwm_led_thread_entry(void *parameter)
{
    rt_uint32_t period, pulse, dir;
    // period = 500000; /* 周 期 为0.5ms， 单 位 为 纳 秒ns */
    period = 20000000; /* 周 期 为20ms， 单 位 为 纳 秒ns */
    dir = 1; /* PWM脉 冲 宽 度 值 的 增 减 方 向 */
    // pulse = 0; /* PWM脉 冲 宽 度 值， 单 位 为 纳秒ns */
    pulse = 10000000; /* PWM脉 冲 宽 度 值， 单 位 为 纳秒ns */

    /* 查 找 设 备 */
    pwm_dev = (struct rt_device_pwm *)rt_device_find(PWM_DEV_NAME);
    if (pwm_dev == RT_NULL)
    {
        rt_kprintf("pwm sample run failed! can't find %s device!\n", PWM_DEV_NAME);
        return;
    }
    rt_kprintf("pwm sample run ! find %s device!\n", PWM_DEV_NAME);
    /* 设 置PWM周 期 和 脉 冲 宽 度 默 认 值 */
    rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, pulse);
    /* 使 能 设 备 */
    rt_pwm_enable(pwm_dev, PWM_DEV_CHANNEL);
    while (1)
    {
        rt_thread_mdelay(1000);
        // rt_thread_mdelay(50);
        // if (dir)
        // {
        //     pulse += 5000; /* 从0值 开 始 每 次 增加5000ns */
        // }
        // else
        // {
        //     pulse -= 5000; /* 从 最 大 值 开 始 每 次减 少5000ns */
        // }
        // if (pulse >= period)
        // {
        //     dir = 0;
        // }
        // if (0 == pulse)
        // {
        //     dir = 1;
        // }
        // /* 设 置PWM周 期 和 脉 冲 宽 度 */
        // rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, pulse);
    }
}

int task_pwm_init(void)
{
    static rt_thread_t tid_pwmled = RT_NULL;

    rt_err_t ret = RT_EOK;

    LOG_I("task_pwm_init");

    /* 创建 serial 线程 */
    tid_pwmled = rt_thread_create("task_pwm",pwm_led_thread_entry, RT_NULL, PWM_THREAD_STACK_SIZE, PWM_THREAD_PRIORITY, PWM_THREAD_TIMESLICE);
   /* 创建成功则启动线程 */
   if (tid_pwmled != RT_NULL)
   {
       rt_thread_startup(tid_pwmled);
   }
   else
   {
       ret = RT_ERROR;
   }

   return ret;

}

//INIT_APP_EXPORT(task_pwm_init);
